﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// A field generator which creates dynamic gridview columns.
/// </summary>
public class LanguageColumnGenerator : IAutoFieldGenerator {
    private string[] languages;
    private string defaultLanguage;

    /// <summary>
    /// Initializes a new <see cref="ColGen"/> to generate columns for the specified languages.
    /// </summary>
    /// <param name="languages">The languages for which to generate columns.</param>
    public LanguageColumnGenerator(string[] languages, string defaultLanguage) {
        this.languages = languages;
        this.defaultLanguage = defaultLanguage;
    }

    #region IAutoFieldGenerator Members

    System.Collections.ICollection IAutoFieldGenerator.GenerateFields(Control control) {
        List<TemplateField> fields = new List<TemplateField>();
        foreach(string lang in languages) {
            TemplateField fld = new TemplateField();
            fld.SortExpression = lang;
            try {
                fld.HeaderText = System.Globalization.CultureInfo.GetCultureInfo(lang).EnglishName + "<br />(<small>" + lang + "</small>)";
            } catch {
                fld.HeaderText = lang;
            }
            fld.ItemTemplate = new GridViewTemplate(ListItemType.Item, lang, lang == defaultLanguage);
            fld.FooterTemplate = new GridViewTemplate(ListItemType.Footer, lang, lang == defaultLanguage);
            fld.ItemStyle.CssClass = "ssCell";
            fields.Add(fld);
        }
        return fields;
    }

    #endregion
}
